package io.jt808.proto.cmd;

import io.norland.buffer.AutoBuffer;
import io.norland.annotations.Proto;
import io.norland.proto.IProto;
import lombok.Data;

/**
 * 下发终端升级包
 */
@Proto("8108")
@Data
public class JT8108 implements IProto {
    /**
     * 更新类别
     */
    private byte updateType;
    /**
     * 生产者ID
     */
    private String manufacturerId;
    /**
     * 版本号长度
     */
    private short versionLength;
    /**
     * 版本号
     */
    private String version;

    /**
     * 升级数据包长度
     */
    private long updateDataLength;
    /**
     * 升级数据
     */
    private byte[] updateData;

    public byte[] writeToBytes() {
        AutoBuffer buffer = new AutoBuffer();
        buffer.writeByte(updateType);
        buffer.writeHexString(manufacturerId, 5);
        buffer.writeByte(versionLength);
        buffer.writeString(version, versionLength);
        buffer.writeInt(updateDataLength);
        buffer.writeBytes(updateData);
        return buffer.array();
    }

    public void readFromBuf(AutoBuffer buf) {
        updateType = buf.readByte();
        manufacturerId = buf.readHexString(5);
        versionLength = buf.readByte();
        version = buf.readString(versionLength);
        updateDataLength = buf.readUnsignedInt();
        updateData = buf.readBytes();
    }

    public String getUpdateType() {
        switch (updateType) {
            case 0:
                return "terminal";
            case 12:
                return "ICCardScanner";
            default://52
                return "BeiDou";
        }
    }

//    public static void main(String[] args) {
//        byte[] bytes = new byte[]{
//                0x01,
//                0x01, 0x0a, 0x0a, 0x0a, 0x0a,
//                0x03,
//                0x02, 0x03, 0x04,
//                0x00,0x00,0x00,0x01,
//                (byte) 0xEE
//        };
//        AutoBuffer buffer = new AutoBuffer(bytes);
//        JT8108 jt8103 = new JT8108();
//        jt8103.readFromBuf(buffer);
//        System.out.println(new String(bytes).equals(new String(jt8103.writeToBytes())));
//    }
}